/*************************************************************************
	> File Name: queryworld.c
	> Author: QiyangXia
	> Mail: 834061456@qq.com 
	> Created Time: 2022年01月24日 星期一 18时16分15秒
 ************************************************************************/
#include "queryword.h"

extern char user_name[];
int inputcnt=0;
int dic()
{
	system("clear");
	time_t t;
	char buf[50]="00-00-00-0000-00-00";
    time(&t);
    ctime_r(&t,buf);   
	//printf("%s\n",user_name);
	
	FILE *pf=NULL;
	int linecnt=0;
	char *pret=NULL;
	struct word *pst=NULL;
	int i=0;
	int ret=0;
	int fd=-1;
	char txtname[30]="xxxxx";
	char allinfo[1024]="";
	int len=0;

	pf=fopen(MODEL_FILE_NAME,"r");
	if(NULL==pf)
	{
		printf("r-读取 %s 失败\n",MODEL_FILE_NAME);
		return -1;
	}
	pret=myreadline(pf);
	while(pret!=NULL)
	{
		linecnt++;
		free(pret);
		pret=myreadline(pf);
	}
	fseek(pf,0,SEEK_SET);
	//printf("%d\n",linecnt);

	pst = (struct word *)malloc(linecnt * sizeof(struct word));
	if(NULL == pst)
	{
		fclose(pf);
		pf = NULL;
		printf("空间建立失败!\n");
		return 2;
	}

	/*读信息*/
	for(i = 0;i < linecnt;i++)
	{
		ret = get_one_model_info(pf,pst+i);
		if(ret != 0)
		{
			break;
		}
	}
	if(i != linecnt)
	{
		fclose(pf);
		pf = NULL;
		free(pst);
		pst = NULL;
		printf("失败\n");
		return 3;

	}

	/*关闭文件*/
	fclose(pf);
	pf = NULL;


	printf("\n\n\033[31m\033[5m\t\t\t请输入您想查询的单词\033[0m\n");
	char a[25]="empty_history_now";
	scanf("%s",a);
	while(getchar()!='\n'){}
	for(i = 0;i < linecnt;i++)
	if(strcmp(a,(pst+i)->w)==0)
	{
		ret=1;
		inputcnt++;
		printf("%s\n",(pst+i)->m);
		printf("\n%s\n",buf);
		
		sprintf(txtname,"%s.log",user_name);
		fd=open(txtname,O_WRONLY | O_APPEND | O_CREAT ,0666);
		if(fd < 0)
		{
			printf("w-open failed\n");
			return 4;
		}
		strcpy(allinfo,user_name);
		strcat(allinfo," : ");
		strcat(allinfo,a);
		strcat(allinfo, " ----at---- ");
		strcat(allinfo,buf);
		
		int cnt=0;
		len=strlen(allinfo);
		cnt=write(fd,allinfo,len);
		close(fd);
		fd=-1;
		if(cnt != len)
		{
			printf("write failed\n");
			return 5;
		}
		break;
	}
	if(ret==0)
	{
		printf("本字典太菜，查不到这个单词\n");
		ret=0;
	}
	

	free(pst);
	pst=NULL;

	return 0;
}


int get_one_model_info(FILE *pf,struct word *pt)
{
	char *pstr = NULL;
	char *p = NULL;
	char *q = NULL;

	pstr = myreadline(pf);
	p = strchr(pstr,' ');
	strncpy(pt->w,pstr,p-pstr);
	
	p++;

	q=strchr(p,'\n');
	strncpy(pt->m,p,q-p);
	free(pstr);
	pstr=NULL;
	return 0;
}

/*Don't forget free return memory*/
char *myreadline(FILE *pf)
{
	int linecnt = 0;
	int oldloc = 0;
	int ret = 0;
	char *pout = NULL;
	char *pret = NULL;

	/*1. 获取指示器指示的字符位置所在行的总字符数----linecnt*/
		/*保存位置指示器的当前指示位置- ftell*/
	oldloc = ftell(pf);

		/*循环读取单个字符直到文件尾或行尾，每读一个字符linecnt++*/
	ret = fgetc(pf);
	while(ret >= 0 && ret != '\n')
	{
		linecnt++;
		ret = fgetc(pf);
	}
	if(ret == '\n')
	{
		linecnt++;//'\n'
	}

		/*恢复位置指示器指示的原位置--fseek*/
	fseek(pf,oldloc,SEEK_SET);

	/*2. 分配linecnt+1字节的内存空间*/

	if(linecnt <= 0)	
	{
		return NULL;
	}

	linecnt++;//'\0'

	pout = (char *)malloc(linecnt);
	if(NULL == pout)
	{
		printf("空间建立失败 ! %s:%d\n",__FILE__,__LINE__);
		return NULL;
	}
	memset(pout,0,linecnt);


	/*3. 读出整行*/
	pret = fgets(pout,linecnt,pf);
	if(NULL == pret)
	{
		free(pout);
		pout = NULL;
		printf("行读取错误! %s:%d\n",__FILE__,__LINE__);
		return NULL;
	}

	/*4. 返回动态空间的首地址*/
	return pout;
}
